import { login, getUserInfo, getUserDetailById } from '@/api/user'
import * as auth from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: auth.getToken() || null,
    userInfo: {},
    routeMenu: []
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      auth.setToken(token)
    },
    delToken (state) {
      state.token = null
      auth.removeToken()
    },
    setUserInfo (state, userInfo) {
      state.userInfo = userInfo
      state.routeMenu = userInfo.roles.menus
    },
    delUserInfo (state) {
      state.userInfo = {}
    }
  },
  actions: {
    logout ({ commit }) {
      commit('delToken')
      commit('delUserInfo')
    },
    async getToken ({ commit }, formData) {
      const token = await login(formData)
      commit('setToken', token)
    },
    async getUserInfo ({ commit }) {
      const data = await getUserInfo()
      const avator = await getUserDetailById(data.userId)
      console.log(data)
      console.log(data.roles.points)
      commit('setUserInfo', { ...data, ...avator })
    }
  }
}
